/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical;

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.model.ProductionUnit;
import nl.cloudfarming.client.model.ProductionUnitAction;
import nl.cloudfarming.client.model.PartField;
import nl.cloudfarming.client.model.FieldPlan;
import nl.cloudfarming.client.planning.PlanAction;
import nl.cloudfarming.client.planning.PlanEventKey;
import nl.cloudfarming.eventbus.BaseGuiEventProducer;
import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXPanel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;

/**
 *
 * @author Timon Veenstra
 */
public class PlanProductionUnitAction extends PlanAction implements ProductionUnitAction {

    private final String DISPLAY_NAME = NbBundle.getMessage(this.getClass(), "field_plan.plan_culture_action.name");
    
    private final PlanningEventProducer planningEventProducer = new PlanningEventProducer();
    
    private JTextField cropTypeTextfield = new JTextField(25);
    private JTextField nameTextField = new JTextField(25);
    private JXDatePicker startDatePicker = new JXDatePicker();
    private JXDatePicker endDatePicker = new JXDatePicker();
    
    private Dialog dlg;
    //Use via getService();
    private TacticalPlanningService service;
   
    public PlanProductionUnitAction() {
        putValue(NAME, DISPLAY_NAME);
    }

    /**
     * Crop en field
     * @param e 
     */
    @Override
    public void actionPerformed(ActionEvent e) {
       
        //
        // populate the panel with the information about what the user is about to save
        //
        JXPanel panel = new JXPanel(new MigLayout("wrap 2"));
        
        panel.add(new JLabel(NbBundle.getMessage(this.getClass(), "field_plan.plan_culture_action.name_textfield")));
        panel.add(nameTextField);
        
        panel.add(new JLabel(NbBundle.getMessage(this.getClass(), "field_plan.plan_culture_action.crop")));
        panel.add(cropTypeTextfield);

        panel.add(new JLabel(NbBundle.getMessage(this.getClass(), "field_plan.plan_culture_action.startDate")));
        panel.add(startDatePicker);

        panel.add(new JLabel(NbBundle.getMessage(this.getClass(), "field_plan.plan_culture_action.endDate")));
        panel.add(endDatePicker);

        //
        // create the dialog and add the ok and cancel buttons
        //
        DialogDescriptor dd = new DialogDescriptor(panel, DISPLAY_NAME);
        dd.setOptions(new Object[]{getOkButton(), getCancelButton()});
        dlg = DialogDisplayer.getDefault().createDialog(dd);
        dlg.setVisible(true);
    }

    /**
     * creates the dialog ok button
     *
     * @return
     */
    private JButton getOkButton() {
        JButton jButtonOK = new JButton();
        jButtonOK.setText(NbBundle.getMessage(this.getClass(), "field_plan.plan_culture_action.ok"));
        jButtonOK.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                dlg.setVisible(false);
                String cropType = cropTypeTextfield.getText();
                FieldPlan fieldPlan = Utilities.actionsGlobalContext().lookup(FieldPlan.class);
                                
                ProductionUnit.Builder cultureBuilder = new ProductionUnit.Builder().setCrop(cropType).setFieldPlan(fieldPlan).setName(nameTextField.getText()).setStartDate(startDatePicker.getDate()).setEndDate(endDatePicker.getDate());
                ProductionUnit c  = getService().createProductionUnit(cultureBuilder);
                
                //TODO make this a selectable field instead of dummy
                //FIXME START DUMMY CODE
                PartField.Builder fieldBuilder = new PartField.Builder().setProductionUnit(c).setAcreage(0.0000027D);
                PartField field = getService().createPartField(fieldBuilder);
                c.getPartFields().add(field);
                c.getFieldPlan().getProductionUnits().add(c);
                //FIXME END DUMMY CODE
                planningEventProducer.triggerEvent(new FieldPlanEvent(c.getFieldPlan(), PlanEventKey.NEW_CULTURE));
            }
        });
        return jButtonOK;
    }

    /**
     * creates the dialog cancel button
     * @return
     */
    private JButton getCancelButton() {
        JButton jButtonCancel = new JButton();
        jButtonCancel.setText(NbBundle.getMessage(this.getClass(), "field_plan.plan_culture_action.cancel"));
        jButtonCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                dlg.setVisible(false);
            }
        });
        return jButtonCancel;
    }

    protected TacticalPlanningService getService() {
        if (service == null) {
            service = new TacticalPlanningService();
        }
        return service;
    }
    
    private class PlanningEventProducer extends BaseGuiEventProducer{

        public PlanningEventProducer() {
            super(PlanEventKey.class);
        }

    }
    

}
